home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / winview / procinfo.bas next >
BASIC Source File  |  1999-09-15  |  7KB  |  179 lines

  1. Attribute VB_Name = "modProcInfoWin95"
  2. Option Explicit
  3.  
  4. Private Declare Function GetModuleHandle Lib "kernel32.dll" _
  5.                     Alias "GetModuleHandleA" _
  6.                     (ByVal lpModuleName As String) As Long
  7.                     
  8. Private Declare Function GetProcAddress Lib "kernel32.dll" _
  9.                     (ByVal hModule As Long, _
  10.                      ByVal lpProcName As String) As Long
  11.  
  12. Private Const MAX_PATH As Long = 260
  13.  
  14. Private Type PROCESSENTRY32
  15.     dwSize As Long          ' Size in bytes of type
  16.     cntUsage As Long        ' Number of references to the process
  17.     th32ProcessID As Long   ' Identifier of the Win32 process
  18.     th32DefaultHeapID As Long ' Identifier of the default heap of the process
  19.     th32ModuleID As Long    ' Module identifier of the process
  20.     cntThreads As Long      ' Number of execution threads started by the process
  21.     th32ParentProcessID As Long ' Identifier of the process that created the process being examined
  22.     pcPriClassBase As Long  ' Base priority of any threads created by this process
  23.     dwflags As Long         ' Reserved, do not use
  24.     szExeFile As String * MAX_PATH  ' Path and filename of the executable file for the process
  25. End Type
  26.  
  27. Private Const MAX_MODULE_NAME32 As Long = 255
  28. Private Const MAX_MODULE_NAME32_1 As Long = MAX_MODULE_NAME32 + 1
  29.  
  30. Private Type MODULEENTRY32
  31.     dwSize As Long          ' Size in bytes of type
  32.     th32ModuleID As Long    ' Module identifier in the context of the owning process
  33.     th32ProcessID As Long   ' Identifier of the Win32 process being examined
  34.     GlblcntUsage As Long    ' Globale usage count on the module
  35.     ProccntUsage As Long    ' Module usage count in the contect of the owning process
  36.     modBaseAddr As Long     ' Base address of the module in the contect of the owning process
  37.     modBaseSize As Long     ' size in bytes of the module
  38.     hModule As Long         ' Handle of the module in the contect of the owning process
  39.     szModule As String * MAX_MODULE_NAME32_1    ' String containing the module name
  40.     szExePath As String * MAX_PATH              ' String containing the location (path) of the module
  41. End Type
  42.  
  43. Private Const TH32CS_SNAPHEAPLIST As Long = &H1&
  44. Private Const TH32CS_SNAPPROCESS As Long = &H2&
  45. Private Const TH32CS_SNAPTHREAD As Long = &H4&
  46. Private Const TH32CS_SNAPMODULE As Long = &H8&
  47. Private Const TH32CS_SNAPALL As Long = TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or _
  48.                                         TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE
  49. Private Const TH32CS_SNAPINHERIT As Long = &H80000000
  50.  
  51. Private Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" _
  52.                     (ByVal dwflags As Long, _
  53.                      ByVal th32ProcessID As Long) As Long
  54.  
  55. Private Declare Function Process32First Lib "kernel32.dll" _
  56.                     (ByVal hSnapShot As Long, _
  57.                      lppe As PROCESSENTRY32) As Boolean
  58.                      
  59. Private Declare Function Process32Next Lib "kernel32.dll" _
  60.                     (ByVal hSnapShot As Long, _
  61.                      lppe As PROCESSENTRY32) As Boolean
  62.                      
  63. Private Declare Function Module32First Lib "kernel32.dll" _
  64.                     (ByVal hSnapShot As Long, _
  65.                      lpme As MODULEENTRY32) As Boolean
  66.                      
  67. Private Declare Function Module32Next Lib "kernel32.dll" _
  68.                     (ByVal hSnapShot As Long, _
  69.                      lpme As MODULEENTRY32) As Boolean
  70.                      
  71.  
  72. Private Declare Sub CopyMemory Lib "kernel32.dll" _
  73.                     Alias "RtlMoveMemory" _
  74.                     (xDes As Any, _
  75.                      xSrc As Any, _
  76.                      ByVal Bytes As Long)
  77.                      
  78. Private Declare Function CloseHandle Lib "kernel32.dll" _
  79.                     (ByVal hWnd As Long) As Long
  80.                      
  81. Public Function SupportsToolHelp() As Boolean
  82.     Dim hKernel As Long
  83.     Dim pProcCreatTH32 As Long
  84.     Dim pProcM32First As Long
  85.     Dim pProcM32Next As Long
  86.     Dim pProcP32First As Long
  87.     Dim pProcP32Next As Long
  88.     Dim pProcT32First As Long
  89.     Dim pProcT32Next As Long
  90.     
  91.     ' obtain the module handle of the kernel to retrieve
  92.     ' addresses of the tool helper functions
  93.     hKernel = GetModuleHandle("KERNEL32.DLL")
  94.     If hKernel <> 0 Then
  95.         pProcCreatTH32 = GetProcAddress(hKernel, "CreateToolhelp32Snapshot")
  96.         pProcM32First = GetProcAddress(hKernel, "Module32First")
  97.         pProcM32Next = GetProcAddress(hKernel, "Module32Next")
  98.         pProcP32First = GetProcAddress(hKernel, "Process32First")
  99.         pProcP32Next = GetProcAddress(hKernel, "Process32Next")
  100.         pProcT32First = GetProcAddress(hKernel, "Thread32First")
  101.         pProcT32Next = GetProcAddress(hKernel, "Thread32Next")
  102.     End If
  103.     
  104.     ' all address must be non-NULL to be successfull
  105.     SupportsToolHelp = CBool(pProcCreatTH32 And _
  106.             pProcM32First And pProcM32Next And _
  107.             pProcP32First And pProcP32Next And _
  108.             pProcT32First And pProcT32Next)
  109. End Function
  110.  
  111. Private Function GetProcessModule(ByVal vlPid As Long, _
  112.                     ByVal vlModuleID As Long, _
  113.                     ByRef rtME As MODULEENTRY32) As Boolean
  114.     Dim hModuleSnap As Long
  115.     Dim tME As MODULEENTRY32
  116.     
  117.     ' take a snapshot of all modules in the specified process
  118.     hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, vlPid)
  119.     If hModuleSnap = -1 Then Exit Function
  120.     
  121.     ' fill the size of the type before using it
  122.     tME.dwSize = Len(tME)
  123.     
  124.     ' walk to module list of the process and find the module of
  125.     ' interest. Copy the information to the buffer pointed to
  126.     ' by rtME so that that it can be returned to the caller.
  127.     If Module32First(hModuleSnap, tME) <> False Then
  128.         Do
  129.             If tME.th32ModuleID = vlModuleID Then
  130.                 CopyMemory rtME, tME, Len(rtME)
  131.                 GetProcessModule = True
  132.                 Exit Do
  133.             End If
  134.         Loop While Module32Next(hModuleSnap, tME) <> False
  135.     End If
  136.     
  137.     CloseHandle hModuleSnap
  138. End Function
  139.  
  140. Private Function GetProcessInfo(ByVal vlPid As Long, _
  141.                     ByRef rtPE As PROCESSENTRY32, _
  142.                     ByRef rtME As MODULEENTRY32) As Long
  143.     Dim hProcSnap As Long
  144.     Dim tPE As PROCESSENTRY32
  145.     
  146.     hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, vlPid)
  147.     If hProcSnap = -1 Then Exit Function
  148.     
  149.     tPE.dwSize = Len(tPE)
  150.         
  151.     If Process32First(hProcSnap, tPE) <> False Then
  152.         Do
  153.             If tPE.th32ProcessID = vlPid Then
  154.                 CopyMemory rtPE, tPE, Len(rtPE)
  155.                 If GetProcessModule(vlPid, tPE.th32ModuleID, rtME) Then
  156.                     GetProcessInfo = 2
  157.                     Exit Do
  158.                 End If
  159.                 GetProcessInfo = 1
  160.                 Exit Do
  161.             End If
  162.         Loop While Process32Next(hProcSnap, tPE) <> False
  163.     End If
  164.     CloseHandle hProcSnap
  165. End Function
  166.  
  167. Public Function GetWin95ModuleName(ByVal vlPid As Long) As String
  168.     Dim tPE As PROCESSENTRY32
  169.     Dim tME As MODULEENTRY32
  170.     
  171.     Select Case GetProcessInfo(vlPid, tPE, tME)
  172.         Case 2
  173.             GetWin95ModuleName = LPSTRToStr(tME.szExePath)
  174.         Case 1
  175.             GetWin95ModuleName = LPSTRToStr(tPE.szExeFile)
  176.     End Select
  177. End Function
  178.  
  179.